import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import Stoplight.StoplightTimeoutListener;


public final class bettingRound
{

  // Remember when the timer was started and how long the
  // timer is running.
  private java.util.Date _timerStart;
  private int _timerDuration;
  private Timer _bettingRoundTimer;
  private int roundNo;
  

  //----------------------------------------
  // Nested classes.
  //
  private final class bettingRoundTimeoutListener
      implements ActionListener
  {

    private bettingRound _owner;
    public bettingRoundTimeoutListener(bettingRound owner)
      {
          _owner = owner;
      }

      public void actionPerformed(ActionEvent e)
      {
          _owner.handleTimeout();
          return;
      }

  }

  public void handleTimeout()
  {
      // Get rid of the current timer.
  	_bettingRoundTimer = null;

      _fsm.Timeout();
      return;
  }
  
	 //add FSM data member (can be private, protected, or public).
	 //variable name does *not* have to be _fsm
	 private bettingRoundFSM _fsm;
	 public bettingRound()
	 {
	   // Create the bettingRound's finite state machine (FSM).
     _fsm = new bettingRoundFSM(this);

     // Uncomment to see debug output.
     _fsm.setDebugFlag(true);
	 }
	 
	 //need enterStartState if startState has entry actions before transitions
	 public void start()
   {
       _fsm.enterStartState();
       return;
   }
	 
   //----------------------------------------
   // State Machine Actions
   //----------------------------------------

   public boolean setRoundNo(int roundNoInput); 
	 public boolean getAntes(); //get antes (if any)
	 public boolean doneAntes(); 
	 public boolean forcedBetsRound();
	 public boolean getForcedBets(); //get forced bets (bring /sb & bb) 
	 public boolean doneForcedBets();
	 public boolean getPlayerAction();
	 public boolean validAllIn();
	 public int getBetNo();
	 public void invalidPlayerAction();
	 public void processAllIn();
	 public void processCheck();
	 public void processCall();
	 public void processFold();
	 public void processFirstBet();
	 public void processSecondBet();
	 public void processThirdBet();
	 public void processFourthBet();
	 public boolean incrRoundNo();
	 public void SetTimer(int duration)
   {
       // Remember when this timer was set and how long it
       // lasts. This information is needed for pausing and
       // continuing.
       _timerStart = new java.util.Date();
       _timerDuration = duration;

       // If the timer already exists, then get rid of it.
       if (_stoplightTimer != null)
       {
           // If the timer is running, then stop it, reset
           // its timeout value and run it again.
           if (_stoplightTimer.isRunning() == true)
           {
               _stoplightTimer.stop();
           }

           _stoplightTimer = null;
       }

       _stoplightTimer = new Timer(_timerDuration,
                                   new StoplightTimeoutListener(this));
       _stoplightTimer.setRepeats(false);
       _stoplightTimer.start();

       return;
   }
	 
   public void PauseTimer()
   {
       // Figure out how much time is left on the timer and
       // use that value when continuing the timer.
       java.util.Date currTime = new java.util.Date();

       // Stop the timer now and do the calculations after.
       if (_stoplightTimer != null &&
           _stoplightTimer.isRunning() == true)
       {
           _stoplightTimer.stop();
       }

       _timerDuration = _timerDuration -
               (int) (currTime.getTime() - _timerStart.getTime());
       if (_timerDuration < 0)
       {
           _timerDuration = 0;
       }
       _timerStart = null;

       return;
   }

   public void ContinueTimer()
   {
       // Pick up where we left off by restarting the timer.
       SetTimer(_timerDuration);

       return;
   }

   public void StopTimer()
   {
       // Stop the timer and clear the timer data.
       if (_stoplightTimer != null &&
           _stoplightTimer.isRunning() == true)
       {
           _stoplightTimer.stop();
       }
       _stoplightTimer = null;
       _timerDuration = 0;
       _timerStart = null;

       return;
   }

